1. 准备
需要安装OpenCV3.x版本,以及OpenBLAS,参考:Mac安装OpenCV, MacOS安装caffe
确认安装brew Cython easydict protobuf
下载py-faster-rcnn源码:
git clone –recursive https://github.com/rbgirshick/py-faster-rcnn.git
2. 编译caffe
修改makefile.config
# USE_CUDNN := 1
CPU_ONLY := 1
USE_OPENCV := 1
OPENCV_VERSION := 3
CUDA_DIR := /usr/local/cuda
BLAS := open
BLAS_INCLUDE := /usr/local/Cellar/openblas/0.2.20/include
BLAS_LIB := /usr/local/Cellar/openblas/0.2.20/lib
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/site-packages/numpy/core/include PYTHON_LIB := /usr/lib
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/Cellar/hdf5 /usr/local/Cellar/opencv/3.2.0/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/Cellar/opencv/3.2.0/lib
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
TEST_GPUID := 0
执行编译:
make -j32 && make pycaffe
3. 编译faster-rcnn
修改文件,替换或删除关于gpu的代码行:
修改./lib/setup.py
...
#CUDA = locate_cuda()
...
...
#self.set_executable('compiler_so', CUDA['nvcc'])
...
...
#Extension('nms.gpu_nms',
#[‘nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
#library_dirs=[CUDA['lib64']],
#libraries=['cudart'],
#language='c++',
#runtime_library_dirs=[CUDA['lib64']],
## this syntax is specific to this build system
## we're only going to use certain compiler args with nvcc and not with
## gcc the implementation of this trick is in customize_compiler() below
#extra_compile_args={'gcc': ["-Wno-unused-function"],
#’nvcc': ['-arch=sm_35',
#’—ptxas-options=-v',
#’-c’,
#’—compiler-options',
#”’-fPIC'"]},
#include_dirs = [numpy_include, CUDA['include']]
#)
执行编译:
cd ./lib
make
4. 运行demo
运行demo前需要修改几个文件,替换或删除关于gpu的代码行:
(1) 修改~/py-faster-rcnn/lib/fast_rcnn/config.py
# Use GPU implementation of non-maximum suppression
# __C.USE_GPU_NMS = True
__C.USE_GPU_NMS = False
(2) 修改~/py-faster-rcnn/tools/test_net.py和 ~/py-faster-rcnn/tools/train_net.py
#caffe.set_mode_gpu()
caffe.set_mode_cpu()
(3) 将~/py-faster-rcnn/lib/setup.py
中,含有nms.gpu_nms
的部分去掉
(4) 修改./lib/fast-rcnn/nms_wrapper.py
,修改如下:
from fast_rcnn.config import cfg
#from nms.gpu_nms import gpu_nms
from nms.cpu_nms import cpu_nms
def nms(dets, thresh, force_cpu=False):
"""Dispatch to either CPU or GPU NMS implementations."""
if dets.shape[0] == 0:
return []
if cfg.USE_GPU_NMS and not force_cpu:
print "nms_wrapper GPU ..."
#return gpu_nms(dets, thresh, device_id=cfg.GPU_ID)
else:
return cpu_nms(dets, thresh)
接下来运行demo即可。
5. 问题
(1) import caffe错误,或者报错fatal error: numpy/arrayobject.h没有那个文件或目录
修改Makefile.config找到PYTHON_INCLUDE:
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
要加一个local,变成:
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/dist-packages/numpy/core/include
(2) 报错AttributeError: 'DataFrame' object has no attribute 'sort_values'
# sort_values is new in version 0.17.0, so check your version of pandas. In the previous versions you should use sort
pip install -U pandas
# 查看pandas版本
>>> import pandas as pd
>>> pd.show_versions()
>>> pd.__version__
(3) 找不到目录,在_init_paths.py
中定义的路径修改:
# 将 this_dir = osp.dirname(__file__) 改为:
this_dir = osp.dirname(osp.abspath(__file__))
参考: